﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Month
{

    public class ServiceAccountMonthTradeProductAnalysis : Benefit.Interface.Month.IAccountMonthTradeProductAnalysis
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountMonthTradeProductAnalysis(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当月所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int month, int year)
        {
            var query = from t in db.AccountMonthTradeProductAnalysis where t.Month == month && t.Year == year select t;
            foreach (Models.Month.AccountMonthTradeProductAnalysis c in query)
            {
                db.AccountMonthTradeProductAnalysis.Remove(c);
            }
            db.SaveChanges();
        }



        /// <summary>
        /// 保存账号月变化情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitMonthAccountProductAnalysis(int month, int year)
        {
            var query = db.Accounts.ToList();
            foreach (Models.Sys.Account account in query)
            {
                #region i为0是多，i为1是空
                for (int i = 0; i < 2; i++)
                {
                    var _query = (from t in db.AccountDayTradeProductAnalysis where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id && t.Direction == i select t).ToList();
                    foreach (Models.Day.AccountDayTradeProductAnalysis instru in _query)
                    {
                        var query2 = (from t in db.AccountMonthTradeProductAnalysis where t.Month == instru.TradeHistory.MPdate && t.Year == instru.TradeHistory.YPdate && t.AccountId == account.Id && t.Direction == i && t.ProductId == instru.ProductId select t).ToList();
                        var query3 = (from t in db.AccountDayTradeProductAnalysis where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id && t.Direction == i && t.ProductId == instru.ProductId select t).ToList();
                        Models.Month.AccountMonthTradeProductAnalysis change = new Models.Month.AccountMonthTradeProductAnalysis();
                        change.AccountId = account.Id;
                        change.Direction = i;
                        change.ProductId = instru.ProductId;
                        if (query2.Count() > 0)//有记录更新
                        {
                            change = query2.First();
                        }
                        if (_query.Count() > 0)
                        {
                            change.AvgPostionTime = query3.Sum(a => a.AvgPostionTime);
                            change.MaxReturn = query3.Sum(a => a.MaxReturn);
                            change.OverStopLoss = query3.Sum(a => a.OverStopLoss);
                            change.PingJinPing = query3.Sum(a => a.PingJinPing);
                        }
                        change.Year = year;
                        change.Month = month;
                        if (query2.Count() == 0)
                        {
                            db.AccountMonthTradeProductAnalysis.Add(change);
                        }
                    }

                }
                #endregion
            }
            db.SaveChanges();
        }
    }
}